<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 23, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Web
{
	use KLF\System\KObject;
	use KLF\System\Errors\KArgumentException;
	use KLF\System\KPropertyContext;
	
	/**
	 *  Class KHttpStatus
	 *
	 *  @package KLF\System\Web
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @property int $StatusCode The code.
	 *  @property string $StatusDescription The meaning behind the code.
	 *  @property string $Status The short string.
	 */
	class KHttpStatus
	{
		const HTTP_STATUS_CONTINUE				= 100;
		const HTTP_STATUS_SWITCH_PROTOCOLS		= 101;
		
		const HTTP_STATUS_OK					= 200;
		const HTTP_STATUS_CREATED				= 201;
		const HTTP_STATUS_ACCEPTED				= 202;
		const HTTP_STATUS_PARTIAL				= 203;
		const HTTP_STATUS_NO_CONTENT			= 204;
		const HTTP_STATUS_RESET_CONTENT			= 205;
		const HTTP_STATUS_PARTIAL_CONTENT		= 206;
		const HTTP_STATUS_WEBDAV_MULTI_STATUS	= 207;
		
		const HTTP_STATUS_AMBIGUOUS				= 300;
		const HTTP_STATUS_MOVED					= 301;
		const HTTP_STATUS_REDIRECT				= 302;
		const HTTP_STATUS_REDIRECT_METHOD		= 303;
		const HTTP_STATUS_NOT_MODIFIED			= 304;
		const HTTP_STATUS_USE_PROXY				= 305;
		const HTTP_STATUS_REDIRECT_KEEP_VERB	= 307;
		
		const HTTP_STATUS_BAD_REQUEST			= 400;
		const HTTP_STATUS_DENIED				= 401;
		const HTTP_STATUS_PAYMENT_REQ			= 402;
		const HTTP_STATUS_FORBIDDEN				= 403;
		const HTTP_STATUS_NOT_FOUND				= 404;
		const HTTP_STATUS_BAD_METHOD			= 405;
		const HTTP_STATUS_NONE_ACCEPTABLE		= 406;
		const HTTP_STATUS_PROXY_AUTH_REQ		= 407;
		const HTTP_STATUS_REQUEST_TIMEOUT		= 408;
		const HTTP_STATUS_CONFLICT				= 409;
		const HTTP_STATUS_GONE					= 410;
		const HTTP_STATUS_LENGTH_REQUIRED		= 411;
		const HTTP_STATUS_PRECOND_FAILED		= 412;
		const HTTP_STATUS_REQUEST_TOO_LARGE		= 413;
		const HTTP_STATUS_URI_TOO_LONG			= 414;
		const HTTP_STATUS_UNSUPPORTED_MEDIA		= 415;
		const HTTP_STATUS_RETRY_WITH			= 449;
		
		const HTTP_STATUS_SERVER_ERROR			= 500;
		const HTTP_STATUS_NOT_SUPPORTED			= 501;
		const HTTP_STATUS_BAD_GATEWAY			= 502;
		const HTTP_STATUS_SERVICE_UNAVAIL		= 503;
		const HTTP_STATUS_GATEWAY_TIMEOUT		= 504;
		const HTTP_STATUS_VERSION_NOT_SUP		= 505;
		
		protected $_rpropStatus;
		
		protected $_propStatusCode;
		
		/**
		 * Change the status code of the KHttpStatus object.
		 * 
		 * @param int $statusCode
		 * @param int $pc
		 * @return int
		 */
		public function propStatusCode($statusCode, $pc = 0)
		{
			if ($pc === KPropertyContext::PROP_SET)
			{
				$newStatus = KHttpStatus::createStatus($statusCode);
				
				$this->_propStatusCode = $newStatus->_propStatusCode;
				$this->_rpropStatus = $newStatus->_rpropStatus;
				$this->_rpropStatusDescription = $newStatus->_rpropStatusDescription;
				
				return $statusCode;
			}
			else
			{
				return $this->_propStatusCode;
			}
		}
		
		protected $_rpropStatusDescription;
		
		/**
		 * Generates a new KHttpStatus object
		 */
		protected function __construct($code, $str, $desc)
		{
			$this->_Status = $str;
			$this->_StatusCode = $code;
			$this->_StatusDescription = $desc;
		}
		
		/**
		 * Static constructor for getting Status code objects.
		 *
		 * @author Kristian Oye <kristianoye@gmail.com>
		 * @since 0.1
		 * @return KHttpStatus An object representing the requested status
		 * @link http://msdn.microsoft.com/en-us/library/aa383887.aspx
		*/
		public static function createStatus($code)
		{
			switch($code)
			{
				case KHttpStatus::HTTP_STATUS_CONTINUE:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_CONTINUE, 
						'HTTP_STATUS_CONTINUE',
						'The request can be continued');
					
				case KHttpStatus::HTTP_STATUS_SWITCH_PROTOCOLS:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_SWITCH_PROTOCOLS, 
						'HTTP_STATUS_SWITCH_PROTOCOLS',
						'The server has switched protocols in an upgrade header.');
					
				case KHttpStatus::HTTP_STATUS_OK:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_OK, 
						'HTTP_STATUS_OK',
						'The request completed successfully.');
					
				case KHttpStatus::HTTP_STATUS_CREATED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_CREATED, 
						'HTTP_STATUS_CREATED',
						'The requested has been fulfilled and resulted in the creation of a new resource.');
					
				case KHttpStatus::HTTP_STATUS_ACCEPTED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_ACCEPTED, 
						'HTTP_STATUS_ACCEPTED',
						'The request has been accepted for processing, but the processing has not been completed.');
					
				case KHttpStatus::HTTP_STATUS_PARTIAL:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_PARTIAL, 
						'HTTP_STATUS_PARTIAL',
						'The returned meta information in the entity-header is not the definitive set available from the originating server.');
					
				case KHttpStatus::HTTP_STATUS_NO_CONTENT:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_NO_CONTENT,
						'HTTP_STATUS_NO_CONTENT',
						'The server has fulfilled the request, but there is no new information to send back.');
				
				case KHttpStatus::HTTP_STATUS_RESET_CONTENT:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_RESET_CONTENT,
						'HTTP_STATUS_RESET_CONTENT',
						'The request has been completed, and the client program should reset the document view that caused the request to be sent to allow the user to easily initiate another input action.');

				case KHttpStatus::HTTP_STATUS_PARTIAL_CONTENT:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_PARTIAL_CONTENT,
						'HTTP_STATUS_PARTIAL_CONTENT',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_WEBDAV_MULTI_STATUS:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_WEBDAV_MULTI_STATUS,
						'HTTP_STATUS_WEBDAV_MULTI_STATUS',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_AMBIGUOUS:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_AMBIGUOUS,
						'HTTP_STATUS_AMBIGUOUS',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_MOVED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_MOVED,
						'HTTP_STATUS_MOVED',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_REDIRECT:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_REDIRECT,
						'HTTP_STATUS_REDIRECT',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_REDIRECT_METHOD:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_REDIRECT_METHOD,
						'HTTP_STATUS_REDIRECT_METHOD',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_NOT_MODIFIED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_NOT_MODIFIED,
						'HTTP_STATUS_NOT_MODIFIED',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_USE_PROXY:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_USE_PROXY,
						'HTTP_STATUS_USE_PROXY',
						'The server has fulfilled the partial GET request for the resource.');

				case KHttpStatus::HTTP_STATUS_REDIRECT_KEEP_VERB:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_REDIRECT_KEEP_VERB,
						'HTTP_STATUS_REDIRECT_KEEP_VERB',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_BAD_REQUEST:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_BAD_REQUEST,
						'HTTP_STATUS_BAD_REQUEST',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_DENIED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_DENIED,
						'HTTP_STATUS_DENIED',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_PAYMENT_REQ:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_PAYMENT_REQ,
						'HTTP_STATUS_PAYMENT_REQ',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_FORBIDDEN:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_FORBIDDEN,
						'HTTP_STATUS_FORBIDDEN',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_NOT_FOUND:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_NOT_FOUND,
						'HTTP_STATUS_NOT_FOUND',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_BAD_METHOD:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_BAD_METHOD,
						'HTTP_STATUS_BAD_METHOD',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_NONE_ACCEPTABLE:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_NONE_ACCEPTABLE,
						'HTTP_STATUS_NONE_ACCEPTABLE',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_PROXY_AUTH_REQ:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_PROXY_AUTH_REQ,
						'HTTP_STATUS_PROXY_AUTH_REQ',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_REQUEST_TIMEOUT:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_REQUEST_TIMEOUT,
						'HTTP_STATUS_REQUEST_TIMEOUT',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_CONFLICT:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_CONFLICT,
						'HTTP_STATUS_CONFLICT',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_GONE:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_GONE,
						'HTTP_STATUS_GONE',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_LENGTH_REQUIRED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_LENGTH_REQUIRED,
						'HTTP_STATUS_LENGTH_REQUIRED',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_PRECOND_FAILED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_PRECOND_FAILED,
						'HTTP_STATUS_PRECOND_FAILED',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_REQUEST_TOO_LARGE:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_REQUEST_TOO_LARGE,
						'HTTP_STATUS_REQUEST_TOO_LARGE',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_URI_TOO_LONG:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_URI_TOO_LONG,
						'HTTP_STATUS_URI_TOO_LONG',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_UNSUPPORTED_MEDIA:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_UNSUPPORTED_MEDIA,
						'HTTP_STATUS_UNSUPPORTED_MEDIA',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_RETRY_WITH:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_RETRY_WITH,
						'HTTP_STATUS_RETRY_WITH',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_SERVER_ERROR:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_SERVER_ERROR,
						'HTTP_STATUS_SERVER_ERROR',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_NOT_SUPPORTED:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_NOT_SUPPORTED,
						'HTTP_STATUS_NOT_SUPPORTED',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_BAD_GATEWAY:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_BAD_GATEWAY,
						'HTTP_STATUS_BAD_GATEWAY',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_SERVICE_UNAVAIL:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_SERVICE_UNAVAIL,
						'HTTP_STATUS_SERVICE_UNAVAIL',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_GATEWAY_TIMEOUT:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_GATEWAY_TIMEOUT,
						'HTTP_STATUS_GATEWAY_TIMEOUT',
						'The server has fulfilled the partial GET request for the resource.');
					
				case KHttpStatus::HTTP_STATUS_VERSION_NOT_SUP:
					return new KHttpStatus(KHttpStatus::HTTP_STATUS_VERSION_NOT_SUP,
						'HTTP_STATUS_VERSION_NOT_SUP',
						'The server has fulfilled the partial GET request for the resource.');
					
				default:
					throw new KArgumentException("Unknown status code: $code");
			}
		}
	}
}
